package com.tensquare.recruit.service;

import com.tensquare.recruit.dao.EnterpriseDao;
import com.tensquare.recruit.pojo.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/23 14:37
 */
@Service
public class EnterpriseService {

    @Autowired
    private EnterpriseDao enterpriseDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * fetch data by rule id
     *
     * @description  增加企业
     * @param
     * @return
     */
    public void add(Enterprise enterprise){
        enterprise.setId(idWorker.nextId()+"");
        enterpriseDao.save(enterprise);
    }

    /**
     * fetch data by rule id
     *
     * @description  企业全部列表
     * @param
     * @return
     */
    public List<Enterprise> findAll(){
        return enterpriseDao.findAll();
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID查询企业
     * @param
     * @return
     */
    public Enterprise findById(String id){
        return enterpriseDao.findById(id).get();
    }

    /**
     * fetch data by rule id
     *
     * @description  修改企业
     * @param
     * @return
     */
    public void update(Enterprise enterprise){
        enterpriseDao.save(enterprise);
    }

    /**
     * fetch data by rule id
     *
     * @description  删除企业
     * @param
     * @return
     */
    public void delete(String id){
        enterpriseDao.deleteById(id);
    }

    /**
     * fetch data by rule id
     *
     * @description  热门企业列表
     * @param
     * @return
     */
    public List<Enterprise> hotList(){
        return enterpriseDao.findByIshot("1");
    }

    /**
     * fetch data by rule id
     *
     * @description  构建查询条件
     * @param
     * @return
     */
    public Specification<Enterprise> createSpecification(Map searchMap){
        return new Specification<Enterprise>(){

            @Override
            public Predicate toPredicate(Root<Enterprise> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                    predicateList.add(cb.like(root.get("name").as(String.class),"%"+(String) searchMap.get("name")+"%"));
                }
                if(searchMap.get("summary")!=null && !"".equals(searchMap.get("summary"))){
                    predicateList.add(cb.equal(root.get("summary").as(String.class),(String) searchMap.get("summary")));
                }
                if(searchMap.get("address")!=null && !"".equals(searchMap.get("address"))){
                    predicateList.add(cb.equal(root.get("address").as(String.class),(String) searchMap.get("address")));
                }
                if(searchMap.get("labels")!=null && !"".equals(searchMap.get("labels"))){
                    predicateList.add(cb.equal(root.get("labels").as(String.class),(String) searchMap.get("labels")));
                }
                if(searchMap.get("coordinate")!=null && !"".equals(searchMap.get("coordinate"))){
                    predicateList.add(cb.equal(root.get("coordinate").as(String.class),(String) searchMap.get("coordinate")));
                }
                if(searchMap.get("ishot")!=null && !"".equals(searchMap.get("ishot"))){
                    predicateList.add(cb.equal(root.get("ishot").as(String.class),(String) searchMap.get("ishot")));
                }
                if(searchMap.get("jobcount")!=null && !"".equals(searchMap.get("jobcount"))){
                    predicateList.add(cb.equal(root.get("jobcount").as(String.class),(String) searchMap.get("jobcount")));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * fetch data by rule id
     *
     * @description  根据条件查询企业列表
     * @param
     * @return
     */
    public List<Enterprise> findSearch(Map searchMap){
        Specification<Enterprise> specification = createSpecification(searchMap);
        return enterpriseDao.findAll(specification);
    }

    /**
     * fetch data by rule id
     *
     * @description  分页条件查询
     * @param
     * @return
     */
    public Page<Enterprise> findSearch(Map searchMap,int page,int size){
        Specification<Enterprise> specification = createSpecification(searchMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return enterpriseDao.findAll(specification,pageRequest);
    }
}
